<?php

namespace app\admin\model\shopro\user;

use app\admin\model\shopro\Common;

class WalletLog extends Common
{
    protected $name = 'shopro_user_wallet_log';

    protected $updateTime = false;

    protected $type = [
        'ext' => 'json'
    ];
    // 追加属性
    protected $append = [
        'event_text'
    ];

    const TYPE_MAP = [
        'money'      => '代金券',
        'score'      => '积分',
        'commission' => '佣金',
        'gold'       => '金币',
        'golden'     => '黄金券',
        'silver'     => '银豆',
        'firepower'     => '火力值'
    ];

    protected $eventMap = [
        'score'      => [
            'signin'           => '签到-赠送积分',
            'replenish_signin' => '签到-补签',
            'activity_gift'    => '活动-赠送积分',
            'score_shop_pay'   => '积分商城-积分支付',
            'order_pay'        => '商城订单-积分抵扣',
            'order_refund'     => '订单退款-退还积分',
            'admin_recharge'   => '后台-积分充值',
            'recharge_gift'    => '线上充值-赠送积分'
        ],
        'money'      => [
            'order_pay'              => '商城订单-代金券支付',
            'order_recharge'         => '线上充值',
            'admin_recharge'         => '后台-代金券充值',
            'recharge_gift'          => '线上充值-赠送代金券',
            'activity_gift'          => '活动-赠送代金券',
            'order_refund'           => '订单退款-退还代金券',
            'transfer_by_commission' => '佣金-转入到代金券'
        ],
        'commission' => [
            'withdraw'          => '提现',
            'withdraw_error'    => '提现失败-返还佣金',
            'reward_income'     => '佣金-收益',
            'reward_back'       => '佣金-退还',
            'transfer_to_money' => '佣金-转出到代金券'
        ],
        'gold'      => [
            'order_pay'              => '商城订单-代金券支付',
            'order_recharge'         => '线上充值',
            'admin_recharge'         => '后台-金币充值',
            'recharge_gift'          => '线上充值-赠送金币',
            'order_refund'           => '订单退款-退还金币',
        ],
        'golden'      => [
            'order_pay'              => '商城订单-代金券支付',
            'order_recharge'         => '线上充值',
            'admin_recharge'         => '后台-黄金券充值',
            'recharge_gift'          => '线上充值-赠送黄金券',
            'order_refund'           => '订单退款-退还黄金券',
        ],
        'silver'      => [
            'order_pay'              => '商城订单-代金券支付',
            'order_recharge'         => '线上充值',
            'admin_recharge'         => '后台-银豆充值',
            'recharge_gift'          => '线上充值-赠送银豆',
            'order_refund'           => '订单退款-退还银豆',
        ],
        'firepower'      => [
            'order_pay'              => '商城订单-代金券支付',
            'admin_recharge'         => '后台-充值',
        ],
        'principal'      => [
            'order_pay'              => '商城订单-代金券支付',
            'admin_recharge'         => '后台-充值',
        ],
    ];

    public function getEventMap()
    {
        return $this->eventMap;
    }

    public function scopeMoney($query)
    {
        return $query->where('type', 'money');
    }

    public function scopePrincipal($query)
    {
        return $query->where('type', 'principal');
    }

    public function scopeCommission($query)
    {
        return $query->where('type', 'commission');
    }

    public function scopeScore($query)
    {
        return $query->where('type', 'score');
    }

    public function scopeGold($query)
    {
        return $query->where('type', 'gold');
    }

    public function scopeGolden($query)
    {
        return $query->where('type', 'golden');
    }

    public function scopeSilver($query)
    {
        return $query->where('type', 'silver');
    }

    public function scopeFirepower($query)
    {
        return $query->where('type', 'firepower');
    }

    public function getEventTextAttr($value, $data)
    {
        return $this->eventMap[$data['type']][$data['event']] ?? '';
    }
    public function user(){
        return $this->hasOne(\app\common\model\User::class,'id','user_id');
    }
}
